package com.runa.monitor.platinterface.service;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.pojo.NBOrder;
import com.runa.monitor.platinterface.extend.nb.vo.SendCmdParamVo;
import com.runa.monitor.platinterface.mqtt.MqttBaseController;
import com.runa.persistence.DateUtil;
import com.runa.persistence.service.CommNbCmdService;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.util.UtilTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.runa.monitor.platinterface.extend.nb.service.NbPlantService.getSendCmdByParamList;

/**
 * 2025/4/7 9:53
 * zhangYB
 */
@Slf4j
@Service
public class FourGSendService {
    @Resource
    private CommNbCmdService commNbCmdService;

    private List<SendCmdParamVo> getRtcCommand() {
        List<SendCmdParamVo> sendCmdParamVoList = new ArrayList<>();
        sendCmdParamVoList.add(new SendCmdParamVo("rtc:", UtilTime.getCurrentTime("yyyyMMddHHmmss")));
        return sendCmdParamVoList;
    }

    public void sendRtcCommand(String topic) {
        try {
            //下发校时
            List<SendCmdParamVo> sendCmdParamVoList = getRtcCommand();
            sendCommand(topic, sendCmdParamVoList);
        } catch (Exception e) {
            log.error("sendRtcCommand: {}", e);
        }
    }


    public void sendCommand(String id, String topic, String hot, String uitv1, String uitv2) {
        boolean heatingPeriod = DateUtil.heatingPeriod(hot);
        sendCommand(id, topic, heatingPeriod, uitv1, uitv2);
    }

    public void sendCommand(String id, String topic, boolean heatingPeriod, String uitv1, String uitv2, String citv1, String citv2, boolean acquisitionLikeWakeupCycle) {
        try {
            //下发校时
            List<SendCmdParamVo> sendCmdParamVoList = getRtcCommand();
            NBOrder nbOrder = commNbCmdService.getNBOrder(id);
            log.debug("查询下发 {} {}", id, JSON.toJSONString(nbOrder));
            if (null != nbOrder) {
                if (nbOrder.getOpen() != null) {
                    sendCmdParamVoList.add(new SendCmdParamVo("set:", nbOrder.getOpen()));
                }
                String acquisition = nbOrder.getAcquisition();
                boolean acquisitionDown = false;
                if (acquisition != null) {
                    if (heatingPeriod) {
                        if (!citv1.equals(acquisition)) {
                            if (!acquisitionLikeWakeupCycle) {
                                sendCmdParamVoList.add(new SendCmdParamVo("citv1:", acquisition));
                                acquisitionDown = true;
                            }
                        } else {
                            log.info("供暖期下发采集周期 {} 与实际采集周期 {} 相同，无需下发", acquisition, citv1);
                        }
                    } else {
                        if (!citv2.equals(acquisition)) {
                            if (!acquisitionLikeWakeupCycle) {
                                sendCmdParamVoList.add(new SendCmdParamVo("citv2:", acquisition));
                                acquisitionDown = true;
                            }
                        } else {
                            log.info("非供暖期下发采集周期 {} 与实际采集周期 {} 相同，无需下发", acquisition, citv2);
                        }
                    }
                }
                String wakeupCycle = nbOrder.getWakeupCycle();
                boolean wakeupCycleDown = false;
                if (wakeupCycle != null) {
                    if (heatingPeriod) {
                        if (!uitv1.equals(wakeupCycle)) {
                            sendCmdParamVoList.add(new SendCmdParamVo("uitv1:", wakeupCycle));
                            if (acquisitionLikeWakeupCycle) {
                                sendCmdParamVoList.add(new SendCmdParamVo("citv1:", wakeupCycle));
                            }
                            wakeupCycleDown = true;
                        } else {
                            log.info("供暖期下发唤醒周期 {} 与实际唤醒周期 {} 相同，无需下发", wakeupCycle, uitv1);
                        }
                    } else {
                        if (!uitv2.equals(wakeupCycle)) {
                            sendCmdParamVoList.add(new SendCmdParamVo("uitv2:", wakeupCycle));
                            if (acquisitionLikeWakeupCycle) {
                                sendCmdParamVoList.add(new SendCmdParamVo("citv2:", wakeupCycle));
                            }
                            wakeupCycleDown = true;
                        } else {
                            log.info("非供暖期下发唤醒周期 {} 与实际唤醒周期 {} 相同，无需下发", wakeupCycle, uitv2);
                        }
                    }

                }
                if (!wakeupCycleDown && !acquisitionDown) {
                    commNbCmdService.clearWakeupCycle(id);
                }
            }
            sendCommand(topic, sendCmdParamVoList);
        } catch (Exception e) {
            log.error("sendCommand: {} {} {} {} {} {}", id, topic, heatingPeriod, uitv1, uitv2, e);
        }
    }

    public void sendCommand(String id, String topic, boolean heatingPeriod, String uitv1, String uitv2) {
        try {
            //下发校时
            List<SendCmdParamVo> sendCmdParamVoList = getRtcCommand();
            NBOrder nbOrder = commNbCmdService.getNBOrder(id);
            log.debug("查询下发 {} {}", id, JSON.toJSONString(nbOrder));
            if (null != nbOrder) {
                if (nbOrder.getOpen() != null) {
                    sendCmdParamVoList.add(new SendCmdParamVo("set:", nbOrder.getOpen()));
                }
                String wakeupCycle = nbOrder.getWakeupCycle();
                if (wakeupCycle != null) {
                    if (heatingPeriod) {
                        if (!uitv1.equals(wakeupCycle)) {
                            sendCmdParamVoList.add(new SendCmdParamVo("citv1:", wakeupCycle));
                            sendCmdParamVoList.add(new SendCmdParamVo("uitv1:", wakeupCycle));
                        } else {
                            log.info("供暖期下发唤醒周期 {} 与实际唤醒周期 {} 相同，无需下发", wakeupCycle, uitv1);
                            commNbCmdService.clearWakeupCycle(id);
                        }
                    } else {
                        if (!uitv2.equals(wakeupCycle)) {
                            sendCmdParamVoList.add(new SendCmdParamVo("citv2:", wakeupCycle));
                            sendCmdParamVoList.add(new SendCmdParamVo("uitv2:", wakeupCycle));
                        } else {
                            log.info("非供暖期下发唤醒周期 {} 与实际唤醒周期 {} 相同，无需下发", wakeupCycle, uitv2);
                            commNbCmdService.clearWakeupCycle(id);
                        }
                    }
                }
            }
            sendCommand(topic, sendCmdParamVoList);
        } catch (Exception e) {
            log.error("sendCommand: {} {} {} {} {} {}", id, topic, heatingPeriod, uitv1, uitv2, e);
        }

    }

    public void sendCommand(String id, String topic, String wakeupCycle) {
        try {
            List<SendCmdParamVo> sendCmdParamVoList = getRtcCommand();

            NBOrder nbOrder = commNbCmdService.getNBOrder(id);
            log.debug("查询下发 {} {}", id, JSON.toJSONString(nbOrder));
            if (null != nbOrder) {
                String downWakeupCycle = nbOrder.getWakeupCycle();
                if (downWakeupCycle != null) {
                    if (!wakeupCycle.equals(downWakeupCycle)) {
                        sendCmdParamVoList.add(new SendCmdParamVo("sample:", downWakeupCycle));
                        sendCmdParamVoList.add(new SendCmdParamVo("report:", downWakeupCycle));
                    } else {
                        log.info("下发唤醒周期 {} 与实际唤醒周期 {} 相同，无需下发", downWakeupCycle, wakeupCycle);
                        commNbCmdService.clearWakeupCycle(id);
                    }
                }
                if (null != nbOrder.getMode()) {
                    sendCmdParamVoList.add(new SendCmdParamVo("mode:", nbOrder.getMode()));
                }
            }
            sendCommand(topic, sendCmdParamVoList);
        } catch (Exception e) {
            log.error("sendMeterCommand: {} {} {} {}", id, topic, wakeupCycle, e);
        }
    }

    private void sendCommand(String topic, List<SendCmdParamVo> sendCmdParamVoList) {
        try {
            //下发校时
            String sendContent = getSendCmdByParamList(sendCmdParamVoList);
            sendContent = "platform," + sendContent + "\r\n";
            log.info("{} 下发： {}", topic, JSON.toJSONString(sendContent));
            MqttBaseController.getConnector().sendMessage(topic, sendContent);
            LogUtil.getLogAndInfo("fourGCmd", "CMD", "{} {}", topic, sendContent);
        } catch (Exception e) {
            log.error("sendCommand: {}", e);
        }
    }


}
